package com.cqs.leetcode.backtrack;

import com.cqs.leetcode.TestCaseUtils;

import java.util.*;

/**
 * @author lixiaowen
 * @create 2019-12-18
 */
public class Permutations46 {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        permutation(nums,0, new ArrayList<>(), new HashSet<>(), result);
        return result;
    }

    private void permutation(int[] nums, int level, List<Integer> list, Set<Integer> used, List<List<Integer>> result ){
        if (level == nums.length) {
            result.add(new ArrayList<>(list));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (used.contains(nums[i])) continue;
            list.add(nums[i]);
            used.add(nums[i]);
            permutation(nums, level+1, list, used, result);
            list.remove(list.size()-1);
            used.remove(nums[i]);
        }
    }

    public static void main(String[] args) {
        int size = 5;
        int[] nums = new int[size];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i+1;
        }
        Permutations46 permutations46 = new Permutations46();
        List<List<Integer>> lists = permutations46.permute(nums);
        System.out.println(lists);
    }
}
